package pATT.profilingAlgorithm.PersonalSearcher;
/***************************************************************************
 *                              ObjectValuePair                            *
 *                       initial version by: Ryan Scherle                  *
 *                      copyright (C) 2000 IGLU Research Group             *
 *                             rscherle@acm.org                            *
 ***************************************************************************/
/***************************************************************************
 * The licence for this code is included in LICENSE.txt.  If you did not   *
 * get this file with this distribution, request a copy from               *
 * iglu@cs.indiana.edu                                                     *
 ***************************************************************************/

import java.io.Serializable;

/**
   A mapping of an object to some value or score. Since this class
   is <code>Comparable</code>, it can be used in conjunction
   with a <code>SortedSet</code> or sorted array to keep objects
   in order of decreasing values. 
   
   @author Ryan Scherle <a href="mailto:rscherle@acm.org">rscherle@acm.org</a>
   @version 1.0
**/

public class ObjectValuePair extends Object
  implements Serializable, Comparable
{
  /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
private Object object;
  private Double value;

  /////////////////////////////////////////////////////////////////
  /**
    Constructs a pair.
  **/
  public ObjectValuePair(Object object, Double value)
  {
    this.object = object;
    this.value = value;
  }

  /////////////////////////////////////////////////////////////////
  /**
    Constructs a pair.
  **/
  public ObjectValuePair(Object object, double value)
  {
    this(object, new Double(value));
  }
  
  /////////////////////////////////////////////////////////////////
  /**
    Returns the object from this pair.
  **/
  public Object getObject()
  {
    return object;
  }

  /////////////////////////////////////////////////////////////////
  /**
    Returns the value from this pair.
  **/
  public Double getValue()
  {
    return value;
  }

  /////////////////////////////////////////////////////////////////
  /**
    Changes the value for this object.
  **/
  public void setValue(Double value)
  {
    this.value = value;
  }
  
  /////////////////////////////////////////////////////////////////
  /**
    Compares this object to another object.
    <code>ObjectValuePair</code>s are only comparable to other
    <code>ObjectValuePair</code>s, so passing any other type of
    object to this method will result in a
    <code>ClassCastException</code>.
  **/
  public int compareTo(Object obj)
  {
//    int result = 0;
    
    if(!(obj instanceof ObjectValuePair))
      {
	throw new ClassCastException(obj + " is not an ObjectValuePair");
      }

    return compareTo((ObjectValuePair) obj);
  }

  /////////////////////////////////////////////////////////////////
  /**
    Compares this object to another object. The comparison is made
    based on the value associated with the object, larger values
    being earlier in the ordering. If two objects have the same value,
    or if they are not <code>Comparable</code>,
    they are compared alphabetically based on their string representations.
    Therefore, this method consideres objects to be equal if their
    values are equal and their string representations are equal. 
  **/
  @SuppressWarnings("unchecked")
public int compareTo(ObjectValuePair obj)
  {
    int result = 0;
    Object otherVal = obj.getValue();
    Object otherObj = obj.getObject();

    if((this.value instanceof Comparable) &&
       (otherVal instanceof Comparable))
      {
	try
	  {
	    result = - ((Comparable)this.value).compareTo(otherVal);
	  }
	catch(ClassCastException e)
	  {
	    result = 0;
	  }
      }
    if(result == 0)
      {
	try
	  {
	    result = ((Comparable)object).compareTo(otherObj);
	  }
	catch(ClassCastException e)
	  {
	    String thisString = this.toString();
	    result = thisString.compareTo(obj.toString());
	  }
      }

    return result;
  }

  /////////////////////////////////////////////////////////////////
  /**
    Returns the string representation of the object.
  **/
  public String toString()
  {
    return "<" + object + "," + value + ">";
  }
  
}